package com.example.travel_front.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.travel_front.Activity.CaregiverDetailActivity;
import com.example.travel_front.Model.Caregiver;
import com.example.travel_front.R;

import java.util.List;

public class CaregiverAdapter extends RecyclerView.Adapter<CaregiverAdapter.ViewHolder> {

    private final List<Caregiver> staffList;
    private final Context context;

    public CaregiverAdapter(List<Caregiver> staffList,Context context) {
        this.staffList = staffList;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_caregiver, parent, false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Caregiver staff = staffList.get(position);
        holder.staffName.setText(staff.getName());
        holder.staffAge.setText("Age: " + staff.getAge() + " years old");
        holder.staffGender.setText("Gender: " + staff.getGender());
        holder.staffLengthOfService.setText("Length of service: " + staff.getLengthOfService() + " years");
        holder.staffInstitution.setText("Institution: " + staff.getInstitution());
        holder.staffSkills.setText("Good at: " + staff.getSkills());
        Glide.with(holder.itemView.getContext()).load(staff.getImageUrl()).error(R.drawable.caregiver).into(holder.staffImage);
        // 绑定数据到视图
        holder.itemView.setOnClickListener(v -> {
            // 处理点击事件
            Intent intent = new Intent(context, CaregiverDetailActivity.class);
            // 可以传递数据
            intent.putExtra("staffId", staff.getId());
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return staffList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView staffImage;
        TextView staffName;
        TextView staffAge;
        TextView staffGender;
        TextView staffLengthOfService;
        TextView staffInstitution;
        TextView staffSkills;

        public ViewHolder(View itemView) {
            super(itemView);
            staffImage = itemView.findViewById(R.id.staffImage);
            staffName = itemView.findViewById(R.id.staffName);
            staffAge = itemView.findViewById(R.id.staffAge);
            staffGender = itemView.findViewById(R.id.staffGender);
            staffLengthOfService = itemView.findViewById(R.id.staffLengthOfService);
            staffInstitution = itemView.findViewById(R.id.staffInstitution);
            staffSkills = itemView.findViewById(R.id.staffSkills);
        }
    }
}

